package com.example.mobile2021606128wje0602;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private EditText edtTxtMainUserName;
    private EditText edtTxtMainPassword;
    private CheckBox chkBoxMainPSW;

    private Button btnMainLogin;

    private SharedPreferences sp;
    private CheckBox chkBoxMainLogin;
    private Button btnMainRegister;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        initView();

        sp = getSharedPreferences( "login",MODE_PRIVATE );

//        chkBoxMainLogin.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//
//
//
//            }
//        } );
        chkBoxMainPSW.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean( "chkPsw", isChecked );
                editor.commit();

            }
        } );
        btnMainLogin.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = edtTxtMainUserName.getText().toString().trim();
                String passWord = edtTxtMainPassword.getText().toString().trim();
                if (userName.equals( "admin" ) && passWord.equals( "123456" )) {

                    if (chkBoxMainPSW.isChecked()) {
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString( "userName", userName );
                        editor.putString( "password",passWord );
                        editor.commit();
                    }
                    Intent intent=new Intent(MainActivity.this,ShowDataActivity.class);
                    intent.putExtra( "PassWord",edtTxtMainPassword.getText().toString().trim());
                    Bundle bundle=new Bundle();
                    bundle.putString( "passWord",edtTxtMainPassword.getText().toString().trim() );
                    intent.putExtras( bundle );

                    startActivity( intent );

                }else{
                    AlertDialog.Builder builder=new AlertDialog.Builder( MainActivity.this );
                    builder.setMessage( "输入的用户名或密码错误！" );
                    builder.setPositiveButton( "确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            MainActivity.this.finish();
                        }
                    } );
                    AlertDialog dialog = builder.create();
                    dialog.show();


                }
//                Intent intent=new Intent(MainActivity.this,ShowDataActivity.class);
//                intent.putExtra( "PassWord",edtTxtMainPassword.getText().toString().trim());
//                 Bundle bundle=new Bundle();
//                 bundle.putString( "passWord",edtTxtMainPassword.getText().toString().trim() );
//                 intent.putExtras( bundle );
//
//                startActivity( intent );

            }
        } );

    }

    @Override
    protected void onStart() {
        super.onStart();
        //读取。赋值给控件
        boolean chkPsw=sp.getBoolean( "chkPsw",false );
        chkBoxMainPSW.setChecked(chkPsw);

        if (chkPsw){
            edtTxtMainPassword.setText(  sp.getString( "passWord",null ));
        }
    }

    private void initView() {
        textView = (TextView) findViewById( R.id.textView );
        edtTxtMainUserName = (EditText) findViewById( R.id.edtTxtMainUserName );
        edtTxtMainPassword = (EditText) findViewById( R.id.edtTxtMainPassword );
        chkBoxMainPSW = (CheckBox) findViewById( R.id.chkBoxMainPSW );

        btnMainLogin = (Button) findViewById( R.id.btnMainLogin );

        chkBoxMainLogin = (CheckBox) findViewById( R.id.chkBoxMainLogin );
        btnMainRegister = (Button) findViewById( R.id.btnMainRegister );
    }
}